package grape.wx.service.mp.reply.impl;

import grape.base.rest.client.dict.DictClient;
import grape.base.service.dict.po.Dict;
import grape.wx.service.mp.reply.api.IMpMsgReplyParser;
import grape.wx.service.mp.reply.po.MpMsgReply;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.builder.outxml.TextBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * Created by yangwei
 * Created at 2020/7/23 20:56
 */
@Order
@Service
public class DefaultMpMsgReplyParserImpl implements IMpMsgReplyParser {

    @Autowired
    private DictClient dictClient;

    @Override
    public boolean support(MpMsgReply reply) {
        return true;
    }

    @Override
    public WxMpXmlOutMessage parse(MpMsgReply reply, WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService, WxSessionManager sessionManager) {
        Dict replayMsgType = dictClient.getDictById(reply.getReplyMsgTypeDictId());
        String xmlMsgType = replayMsgType.getCode().replace(MpMsgReply.xml_msg_type_dict_item_prefix, "");
        switch (xmlMsgType){
            case WxConsts.XmlMsgType.TEXT: {
                return WxMpXmlOutMessage.TEXT()
                        .content(reply.getReplyContent())
                        .fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser())
                        .build();
            }
        }
        return null;
    }
}
